from selenium.common import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


def wait_for_text1(driver,by,value):
    driver.implicitly_wait(1)

    res = driver.find_elements(by, value)
    if res == []:
        return False
    else:
        return True

# 显示等待:等待一个元素的出现
def wait_for_text2(self, text):
    try:
        WebDriverWait(self.driver, 5).until(lambda x: x.find_element(By.XPATH, f"//*[@text='{text}']"))
        return True
    except:
        return False


# 通过显式等待寻找元素，如果在规定的时间没有找到，会抛出一个TimeoutException异常
# 捕获了这个异常返回一个false
def wait_for_text(driver,text,time=5):
    try:
        WebDriverWait(driver,time).until(
            expected_conditions.visibility_of_element_located((By.XPATH,f"//*[@text='{text}']")),
            message=f"{text}没找到"
        )
        return True
    except TimeoutException:
        return False
